<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 7:55
 */

namespace Admin\Controller;

use Think\Page;

class VideoController extends AdminController
{
    public function index()
    {
        $p = I('p', '1');
        $video = M('Video');
        $page_size = 10;
        $count = $video->count();
        $list = $video->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }


    public function add()
    {
        $this->display();
    }

    public function edit()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $where = array('id' => $id);
            $video = M('Video')->where($where)->select();
            if ($video) {
                $this->assign('video', $video);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->error('查找失败');
        }
    }

    public function update()
    {
        $id = I('id', '');
        $title = I('title', '');
        $url = I('url', '');
        $show = I('show', '0');
        $create_time = date('Y-m-d H:i:s');

        $data['title'] = $title;
        $data['url'] = $url;
        $data['show'] = $show;
        $data['create_time'] = $create_time;

        $video = M('Video');

        if (empty($id)) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728;// 设置附件上传大小
            $upload->exts = array('mp4');// 设置附件上传类型
            $upload->savePath = '/Video/'; // 设置附件上传目录
            $info = $upload->upload();
            if ($info) {
                $url = $info['savepath'] . $info['savename'];
                $data['url'] = $url;
            } else {
                // 上传错误提示错误信息
                $this->error($upload->getError());
            }
            $res = $video->data($data)->add();
            if ($res) {
                $this->success("添加成功");
            } else {
                $this->error("添加失败");
            }
        } else {
            $data['id'] = $id;
            $res = $video->data($data)->save();
            if ($res) {
                $this->success("修改成功");
            } else {
                $this->error("修改失败");
            }
        }
    }

    public function active()
    {
        $id = I('id', '');
        $show = I('show', '');
        $where = array('id' => $id);
        if (empty($id) || $show == '1' || $show == '0') {
            $count = M('Video')->where($where)->count();
            if ($count == 0) {
                $this->error("不存在该记录");
            } else {
                $res = M('Video')->where($where)->data(array('show' => $show))->save();
                if ($res) {
                    $this->success("状态修改成功");
                } else {
                    $this->error("状态修改失败");
                }
            }
        } else {
            $this->error("不存在该记录");
        }
    }

    public function delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Video')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功!");
            } else {
                $this->error("删除失败！");
            }
        } else {
            $this->error("删除失败");
        }
    }

}